package com.prestamomo.vil.ppa;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustAttribution;
import com.adjust.sdk.AdjustConfig;
import com.adjust.sdk.LogLevel;
import com.adjust.sdk.OnAttributionChangedListener;
import com.adjust.sdk.OnDeviceIdsRead;
import com.flurry.android.FlurryAgent;
import com.flurry.android.FlurryPerformance;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.text.SimpleDateFormat;
import java.util.Date;

public class PmvilApp extends Application {
    static {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                return new ClassicsHeader(context);
            }
        });
        ClassicsHeader.REFRESH_HEADER_PULLING = "下拉重新整理";
        ClassicsHeader.REFRESH_HEADER_REFRESHING = "正在刷新...";
        ClassicsHeader.REFRESH_HEADER_LOADING = "正在加載...";
        ClassicsHeader.REFRESH_HEADER_RELEASE = "釋放立即刷新";
        ClassicsHeader.REFRESH_HEADER_FINISH = "重新整理完成";
        ClassicsHeader.REFRESH_HEADER_FAILED = "刷新失敗";
        ClassicsHeader.REFRESH_HEADER_UPDATE =  new SimpleDateFormat("HH:mm").format(new Date());
    }
    public static PmvilApp pmvilApss;
    public static PmvilApp getPmvilApss(){return pmvilApss;}
    public static String pmvilTrackerName ="Organic";
    public static String pmvilAdid ="112233111111111";
    public static String pmvilGoogleAdid="099877833-7338289-37272";

    @Override
    public void onCreate() {
        super.onCreate();
        pmvilApss = this;
    }

    private void pmvilInitAdjust(){
        AdjustConfig pmvilAjustCfg = new AdjustConfig(this, "dyas73n4o000", AdjustConfig.ENVIRONMENT_PRODUCTION, !BuildConfig.DEBUG);
        if(BuildConfig.DEBUG){
            pmvilAjustCfg.setLogLevel(LogLevel.VERBOSE);
        }else{
            pmvilAjustCfg.setLogLevel(LogLevel.SUPRESS);
        }
        pmvilAjustCfg.setUrlStrategy(AdjustConfig.URL_STRATEGY_CHINA);
        Adjust.onCreate(pmvilAjustCfg);

        registerActivityLifecycleCallbacks(new PmvilAdjsApplica());

        pmvilAjustCfg.setOnAttributionChangedListener(new OnAttributionChangedListener() {
            @Override
            public void onAttributionChanged(AdjustAttribution adjustAttribution) {
                pmvilAdid = adjustAttribution.adid;
                pmvilTrackerName = adjustAttribution.trackerName;
            }
        });

        Adjust.getGoogleAdId(getApplicationContext(), new OnDeviceIdsRead() {
            @Override
            public void onGoogleAdIdRead(String s) {
                pmvilGoogleAdid = s;
            }
        });
    }

    private static final class PmvilAdjsApplica implements Application.ActivityLifecycleCallbacks{
        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {

        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {
            Adjust.onResume();
        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {
            Adjust.onPause();
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {

        }
    }
    public static String getPmAdjustAdidOrName(boolean pmvilIsAdid){
        if(pmvilIsAdid){
            if (TextUtils.isEmpty(pmvilAdid)) {
                AdjustAttribution pmvilAttribution = Adjust.getAttribution();
                if (pmvilAttribution == null) {
                    return pmvilAdid;
                }
                pmvilAdid = pmvilAttribution.adid;
            }
            return pmvilAdid;
        }else{
            if (TextUtils.isEmpty(pmvilTrackerName)) {
                AdjustAttribution pmvilAdjAttribution = Adjust.getAttribution();
                if (pmvilAdjAttribution == null) {
                    return pmvilTrackerName;
                }
                pmvilTrackerName = pmvilAdjAttribution.trackerName;
            }
            return pmvilTrackerName;
        }
    }
    public static String getPmAdjustAdid(){
       return getPmAdjustAdidOrName(true);
    }

    public static String getPmAdjustTrsName(){
       return getPmAdjustAdidOrName(false);
    }

    private void pmvilInitFlurry(){
       new FlurryAgent.Builder().withDataSaleOptOut(false)
               .withCaptureUncaughtExceptions(true)
               .withIncludeBackgroundSessionsInMetrics(true)
               .withLogLevel(Log.ERROR)
               .withPerformanceMetrics(FlurryPerformance.ALL)
               .withLogEnabled(true)
               .build(this,"flurry_key");
    }
}
